﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Media;
using System.Threading;

namespace SCG.WinForms
{
    public class SoundManager
    {
        //Register a sound
        public void LoadSound(string soundName, Stream stream)
        {
            SoundPlayer sp = new SoundPlayer(stream);
            _players[soundName] = sp;
        }
        public void LoadSound(string soundName, string fileName)
        {
            SoundPlayer sp = new SoundPlayer(fileName);
            _players[soundName] = sp;
        }

        //Play a sound
        public void PlaySound(string soundName)
        {
            if (Mute)
                return;
            try
            {
                if (_currentSound != null)
                    _currentSound.Stop();
                if (!(_players.ContainsKey(soundName)))
                    LoadSound(soundName, soundName);
                _currentSound = _players[soundName];
                _currentSound.Play();             
            }
            catch
            {
            }
        }

        //Mute the player?
        public bool Mute
        {
            get
            {
                return _mute;
            }
            set
            {
                _mute = value;
            }
        }

        //private members
        private bool _mute = false;
        private Dictionary<string, SoundPlayer> _players = new Dictionary<string, SoundPlayer>();
        private SoundPlayer _currentSound = null;
    }
}
